/**
 * @file
 * @auth jinguangguo
 * @date 2017/1/18
 */

import Vue from 'vue';
import tpl from './tpl.html';
import commonService from '../../../common/commonService';
import service from './service'
import filter from '../../../../grsc-base/util/filter';
import lib from '../../../common/lib';
import Popover from '../../../../grsc-base/component/Popover.vue';
import Loading from '../../../../grsc-base/component/Loading.vue';
import NoData from '../../../../grsc-base/component/NoData.vue';
import Pager from '../../../../grsc-base/component/Pager.vue';
import MathTool from '../../../../grsc-base/util/mathtool';
import lockDialog from '../component/lockDialog/main';
import unlockDialog from '../component/unlockDialog/main';
import setPayPwd from '../../../component/setPayPwd/main';


Vue.prototype.$amountAvailable = 0;
Vue.prototype.$coinTypeText = 0;

export default Vue.extend({
    name: "LockPlan",
    template: tpl,
    components: {
        Popover,
        Loading,
        NoData,
        Pager,
        setPayPwd,
        lockDialog,
        unlockDialog
    },

    data() {
        return {
            filter: filter,
            coinTypeText: 'ACT',
            precision: 0,

            isCanLock: false,
            isCanUnlock:  false,
            amount: 0,
            lockAmount: 0,
            lastRewardAmount: 0,
            totalRewardAmount: 0,
            beginDate: null,
            endDate: null,

            isLoading: true,
            isLock: true,
            isSetPayPassword: true,
            isShowSetPayPwd: false,
            isShowLockDialog: false,
            isShowUnlockDialog: false,
            lockList: [],
            isLockListLoading: true,
            condition: {
                pageNo: 1,
                pageSize: 10,
                pageNum: 1
            }
        }
    },
    created() {
        this.coinTypeText = 'ACT';
        this.fetchPayPsdStatus();
        this.fetchLockList();
        this.getLockPositionInfo();
    },
    mounted(){
    },
    methods: {
        getLockPositionInfo() {
            this.isLoading = true;
            //this.precision = coinConfig.assets[this.coinTypeText].withdrawPrecision;

            service.lockPositionInfo({assetCode:this.coinTypeText}).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.amount = filter.floorFix(rep.data.amount, this.precision);
                    this.lockAmount = filter.floorFix(rep.data.lockAmount, this.precision);
                    this.lastRewardAmount = filter.floorFix(rep.data.lastRewardAmount, this.precision);
                    this.totalRewardAmount = filter.floorFix(rep.data.totalRewardAmount, this.precision);
                    this.beginDate = rep.data.beginDate;
                    this.endDate = rep.data.endDate;
                    let beginDate = new Date(this.beginDate);
                    let endDate = new Date(this.endDate);
                    let today = new Date();

                    if(today<beginDate || today>endDate){
                        this.isCanLock = false;
                    }else{
                        this.isCanLock = true;
                    }

                    if(this.lockAmount<=0){
                        this.isCanUnlock = false;
                    }else{
                        this.isCanUnlock = true;
                    }
                    

                    this.isLoading = false;
                } else {
                    window.error(rep.msg)
                }
            })
        },
        fetchPayPsdStatus(){
            commonService.basic().done(rep=>{
                if(rep.code === window.CODE.SUCCESS){
                    this.isSetPayPassword = rep.data.setPayPassword;
                }else {
                    window.toast(rep.msg);
                }
            })
        },
        showLockDialog() {
            let beginDate = new Date(this.beginDate);
            let endDate = new Date(this.endDate);
            let today = new Date();
            if(today<beginDate || today>endDate){
                return;
            }


            this.isLock = true;
            if(this.isSetPayPassword === false){
                this.isShowSetPayPwd = true;
            }else {
                this.isShowLockDialog = true;
            }
        },
        showUnlockDialog() {
            this.isLock = false;
            if(this.isSetPayPassword === false){
                this.isShowSetPayPwd = true;
            }else {
                this.isShowUnlockDialog = true;
            }
        },
        setPayPwdSuccess() {
            if(this.isLock){
                this.isShowLockDialog = true;
            }else{
                this.isShowUnlockDialog = true;
            }
        },
        lockSuccess() {
            this.isShowLockDialog = false;
            this.getLockPositionInfo();
            this.fetchLockList();
        },
        unlockSuccess() {
            this.isShowUnlockDialog = false;
            this.getLockPositionInfo();
            this.fetchLockList();
        },
        /**
         * 获取锁仓操作记录
         * */
        fetchLockList(){
            this.isLockListLoading = true;
            let param = {
                pageNo: this.condition.pageNo,
                pageSize: this.condition.pageSize,
                assetCode: this.coinTypeText
            };
            service.lockPositionList(param).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.lockList = rep.data.list;
                    this.condition.pageNum = Number(filter.ceilFix(rep.data.total / this.condition.pageSize, 0));
                } else {
                    window.error(rep.msg)
                }
                this.isLockListLoading = false;
            })
        },

        /**
         * 分页change
         * @param currentPage
         */
        changePage(currentPage) {
            this.condition.pageNo = currentPage;
            this.fetchLockList();
        },

        lock() {
            this.isShowLockDialog = true;
        }

    },
    watch: {}
});
